<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class APPS_Controller extends CI_Controller {
	public $site;
	
	function __construct() 	{
		//parent::CI_Controllers();
		parent::__construct();
		$this->obj =& get_instance();
		$this->load->model('siteModel');

		if ( $this->session->userdata('site') ) {
			$this->site  = $this->session->userdata('site');
		} else {
			$this->site['is_auth']  = $this->session->userdata('is_auth') ? $this->session->userdata('is_auth') : false; 
			$this->site['user_name']= $this->session->userdata('user_name') ? $this->session->userdata('user_name') : "";
			$this->site['param']    = $this->getParam();  
			$this->site['title'] 	= $this->site['param']['site_title'] ;
			$this->site['host']	 	= $this->config->item("host");
			$this->site['site']	 	= $this->config->item("base_url");
			//print_r($this->site);
			$this->session->set_userdata('site', $this->site);
		}
		$menu = $this->getMenu();
		//print_r($menu);
		$this->smarty->assign("menu", $menu );		
		
		$locked = array('admin','user');
		$_class = strtolower(get_class($this));
		
		if ( !$this->is_auth() && in_array($_class, $locked)) 		{
			$_isRedirect = true;	
			$_redirect = ($_class == 'admin') ? 'admin' : '' ;
			
			if ($_class == 'admin') {
				$segs = $this->uri->segment_array();
				if ( (count($segs) == '1') || ( (count($segs) == '2' && ( $segs[2] == 'index' || $segs[2] == 'clear') ) )  ) {
					$_isRedirect = false;
				}
			} 
			if ($_isRedirect)  redirect($_redirect);
		}
	}

	public function is_auth() {
		$is_logged = FALSE;
		
		if ($this->obj->session) {
			if ($this->obj->session->userdata('is_auth')) {
				$is_logged = TRUE;
			}
		}
		return $is_logged;
	}
   
   	public function getParam() {
		$tmp = array();
		$rs	 = $this->siteModel->getParam();
		if (count($rs) > 0 ) {
			foreach ($rs as $key=>$value) {
				$tmp[$value['param']] =  $value['value'];
			}
		}
		return $tmp;
	}

	function setPaging($page, $limit, $count) {
			$pg = array();
			$pg['f']	= 1;
			$pg['r']	= $limit;
			$pg['t']	= $count[0]['count'];
			$pg['l']	= ceil($pg['t']/$pg['r']);
			$pg['c']	= $page;
			$pg['p']	= $pg['c'] > 1 ? $pg['c'] - 1 : 1;
			$pg['n']	= $pg['c'] + 1 == $pg['l'] ? $pg['l'] : $pg['c'] + 1;
			$pg['n']	= $pg['n'] > $pg['l'] ? $pg['l'] : $pg['n'];
			$offset	= $pg['r'] * $pg['c'] - $pg['r'];

			$this->data['limit']  = $limit;
			$this->data['page']   = $page;
			$this->data['offset'] = $offset;
			$this->data['paging'] = $pg;

			return $offset;
	}

	
	function getMenu() {
		$data 	= array();
		$rows	= $this->siteModel->getMenu();
		
		if($rows) {
			$i = 0;
			foreach ($rows as $k=>$v) {
				$data[$i]['sectionID'] 	= $v['sectionID'];
				$data[$i]['title'] 		= $v['title'];
				$data[$i]['alias'] 		= $v['alias'];
				$data[$i]['link'] 		= $v['link'];

				$rowsChild	= $this->siteModel->getMenuChild($v['sectionID']);
				if($rowsChild) {
					$x = 0;
					foreach ($rowsChild as $k1=>$v1) {
						$data[$i]['child'][$x]['categoryID'] 	= $v1['categoryID'];
						$data[$i]['child'][$x]['title'] 		= $v1['title'];
						$data[$i]['child'][$x]['alias'] 		= $v1['alias'];
						$data[$i]['child'][$x]['link'] 			= $v1['link'];
						$x++;
					}
				}
				$i++;
			}
		}
		
		return $data;
	
	} 	
	
	
	function showData() 	{
		echo "&nbsp;&nbsp;&nbsp;<b>Data </b>::<br>";
		if ($this->data > 0 ) {
			foreach ($this->data as $key=>$value) {
				echo "&nbsp;&nbsp;&nbsp;". $key ." =  ". $value ."<br>";	
				if (is_array($value) ) {
					foreach ($value as $key1=>$value1) {
						echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;". $key1 ." =  ". $value1 ."<br>";	
						if (is_array($value1) ) {
							foreach ($value1 as $key2=>$value2) {
								echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;". $key2 ." =  ". $value2 ."<br>";	
							}
						} 
					}
				} 
			}
		} else {
			echo "No DATA Available / DATA NOT FOUND";
		}
		echo "<br>";
	} 
	
	function clear(){
		$this->session->unset_userdata('site');
		$this->session->unset_userdata('data');
		echo 'clear() session Data<br>';
	}

	function sentEmail($data){
			//print_r($data);
            $smtp = Array(
						 'protocol' => 'smtp',
						 'smtp_host' => 'ssl://smtp.googlemail.com',
						 'smtp_port' => 465,
						 'smtp_user' => 'noreply@thera.co.id',
						 'smtp_pass' => 'noreply123',
						 'mailtype'  => 'html',
						 'charset'   => 'iso-8859-1',
						 'newline'   => '\r\n'
					);


			$this->load->library('email', $smtp);
			$this->email->set_newline("\r\n");
			//$this->email->from($data['from']);
			$this->email->from('noreply@amg.co.id', 'AMG Info');
			//$this->email->from('noreply@thera.co.id', 'AMG Info');
			$this->email->to($data['to']);
			$this->email->subject($data['subject']);
			$this->email->message($data['message']);

			if ( !$this->email->send()) {
				$return   = FALSE;
			} else {
				$return   = TRUE;
			}
			return $return;
	}

	function sentSMS($data){
        $message = $this->messageSMS($data);

        //$gatewayURL  = 'http://localhost:9333/ozeki?';
        $gatewayURL  = 'http://remo.homeunix.org:9333/ozeki?';
        $request = 'login=admin';
        $request .= '&password=abc123';
        $request .= '&action=sendMessage';
        $request .= '&messageType=SMS:TEXT';
        $request .= '&recepient='.urlencode('+62819642654');
        $request .= '&messageData='.urlencode($message);

        $url =  $gatewayURL . $request;

        file($url);
    }
	
	function message($data){
        $str= "Hi,<br />
			Node ID : ".$data['nodeID']." is DOWN at <strong> ".$data['dateTime']." </strong> <br />
			<br />
			<a href='http://remo.homeunix.org/remoadmin/'>http://remo.homeunix.org/remoadmin/</a>
			<br />
			<br />
			Terima Kasih
            Remo Admin";
        return $str;        
    }

    function messageSMS($data){
        $str= "Hi, Node ID : ".$data['nodeID']." is DOWN at ".$data['dateTime']." 
		Terima Kasih
        Remo Admin";
        return $str;
    }
   

}